package com.example.text;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.lang.reflect.Field;

public class MainActivity extends AppCompatActivity {

    private Button button;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
        textView = findViewById(R.id.textView);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = new String();
                try {
                    s = getText();
                    textView.setText(getJson.getRandomDiary());
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public String getText() throws IllegalAccessException{
        Field[] fields = R.string.class.getFields();
        int resId = fields[(int)(Math.random()*(fields.length-1)+1)].getInt(R.string.class);
        String s = getResources().getString(resId);
        return  s;
    }
}